﻿<%@ Page Title="" Language="C#" MasterPageFile="~/AdminCP/Layouts/Admin.Master" AutoEventWireup="true"
    CodeBehind="CreateBill.aspx.cs" Inherits="MLM.AdminCP.Pages.CreateBill" meta:resourcekey="PageResource1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
        #Pager { float: right; }
        #Pager select { margin-top: 5px; padding: 2px; border: solid 1px #ccc; width: 80px; }
        #ProductsTbl td, #ListProductInBill td { text-align: center; }
        #SelectProductToBill { margin-top: 3px; margin-left: 0; }
        #ProductsTbl img { width: 50px; }
        .ProductThumbnail { width: 50px; }
        #ListProductInBill input[type="text"] { width: 50px; border: solid 1px #ccc; padding: 2px; }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Label runat="server" ID="lbBillCreate" Text="Create Bill" meta:resourcekey="lbBillCreateResource1"></asp:Label>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <h3>
        <asp:Label runat="server" ID="lbselectproduct" Text="Select Product" meta:resourcekey="lbselectproductResource1"></asp:Label></h3>
    <table class="MLMInfoTable" cellpadding="0" cellspacing="0" id="ProductsTbl">
        <tr>
            <th>
            </th>
            <th>
                <asp:Label runat="server" ID="lbThumbnail" Text="Thumbnail" meta:resourcekey="lbThumbnailResource1"></asp:Label>
            </th>
            <th>
                <asp:Label runat="server" ID="lbProductId" Text="Product Id" meta:resourcekey="lbProductIdResource1"></asp:Label>
            </th>
            <th>
                <asp:Label runat="server" ID="lbName" Text="Name" meta:resourcekey="lbNameResource1"></asp:Label>
            </th>
            <th>
                <asp:Label runat="server" ID="lbprice" Text="Price (&euro;)" meta:resourcekey="lbpriceResource1"></asp:Label>
            </th>
            <th>
                <asp:Label runat="server" ID="lbpoint2" Text="Point (PV)" meta:resourcekey="lbpoint2Resource1"></asp:Label>
            </th>
        </tr>
    </table>
    <div id="Pager">
    </div>
    <span id="SelectProductToBill" class="button" onclick="AddProductToBill();">
        <asp:Label ID="lbAddtoBill" runat="server" Text="Add to Bill" meta:resourcekey="lbAddtoBillResource1"></asp:Label></span>
    <h3>
        <asp:Label runat="server" ID="lblistofproducts" Text="List Of Products in bill" meta:resourcekey="lblistofproductsResource1"></asp:Label></h3>
    <table id="ListProductInBill" class="MLMInfoTable" cellpadding="0" cellspacing="0">
        <tr>
            <th>
                <asp:Label runat="server" ID="lbThumbnail2" Text="Thumbnail" meta:resourcekey="lbThumbnail2Resource1"></asp:Label>
            </th>
            <th>
                <asp:Label runat="server" ID="lbproductid2" Text="Product Number" meta:resourcekey="lbproductid2Resource1"></asp:Label>
            </th>
            <th>
                <asp:Label runat="server" ID="lbName2" Text="Name" meta:resourcekey="lbName2Resource1"></asp:Label>
            </th>
            <th>
                <asp:Label runat="server" ID="lbprice2" Text="Price (&euro;)" meta:resourcekey="lbprice2Resource1"></asp:Label>
            </th>
            <th>
                <asp:Label runat="server" ID="lbPoint" Text="Point (PV)" meta:resourcekey="lbPointResource1"></asp:Label>
            </th>
            <th>
                <asp:Label runat="server" ID="lbQuantity" Text="Quantity" meta:resourcekey="lbQuantityResource1"></asp:Label>
            </th>
        </tr>
    </table>
    <br />
    <asp:Label runat="server" ID="lbUserID" Text="User ID (Buyer):" meta:resourcekey="lbUserIDResource1"></asp:Label>
    <input type="text" id="UserIDTxt" style="padding: 2px; border: solid 1px #ccc;" />
    <asp:Label runat="server" ID="Label1" Text="Depo ID (Seller):" meta:resourcekey="lbUserIDResource3232"></asp:Label>
    <input type="text" id="DepoIDTxt" style="padding: 2px; border: solid 1px #ccc;" />
    <span id="CreateBillBtn" class="button" onclick="CreateBill();">
        <asp:Label ID="lbCreateBill" runat="server" Text="Create Bill" meta:resourcekey="lbCreateBillResource1"></asp:Label></span>
    <script type="text/javascript">
        var CurrentPageIndex = 1;
        var PageSize = 10;
        function GetListProduct() {
            $.ajax({
                type: "POST",
                url: "/AdminCP/Pages/CreateBill.aspx/GetListProduct",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function () {
                    $('body').append('<div id="loadingDiv" style="position:fixed; top:50%; left:50%; width:200px;text-align:center; padding:3px; border:solid 1px #ccc; background:#f1f1f1;">Loading data, please wait...</div>');
                },
                data: "{'_pageIndex':'" + CurrentPageIndex + "','_pageSize':'" + PageSize + "'}",
                async: true,
                success: function (msj) {
                    $('#loadingDiv').remove();
                    var data = msj.d;
                    if (typeof (data) == "string") {
                        data = JSON.parse(data);
                    }
                    $('.ProductItem').remove();
                    $.each(data.Products, function (i, item) {
                        $('#ProductsTbl tr:first').after('<tr productid="' + item.ProductId + '" class="ProductItem"><td><input type="checkbox" productid="' + item.ProductId + '"/></td><td><img src="' + item.Thumbnail + '" class="ProductThumbnail"/></td><td><span class="ProductNumber">' + item.ProductNumber + '</span></td><td><span class="ProductName">' + item.Name + '</span></td><td><span class="ProductPrice">' + item.Price + '</span></td><td><span class="ProductPoint">' + item.Point + '</span></td></tr>');
                    });
                    WrapPaging(data.TotalRow, CurrentPageIndex, PageSize, '#Pager', function (p, n) {
                        CurrentPageIndex = p;
                        GetListProduct();
                    });
                }
            });
        }
        $(document).ajaxStop(function () {
            $('#loadingDiv').remove();
        });
        function WrapPaging(numRow, PageIndex, PageSize, objContainer, callBack) {
            var btnNext = $(objContainer).attr("id") + "_btnNextPage";
            var btnPrev = $(objContainer).attr("id") + "_btnPrevPage";
            var selectboxPage = $(objContainer).attr("id") + "_pageListOption";
            var sizePage = $(objContainer).attr("id") + "_sizeListOption";
            var numPage = numRow / PageSize;

            if (PageSize >= numRow) PageIndex = 1;

            if (numPage > Math.floor(numPage))
                numPage = Math.floor(numRow / PageSize) + 1;

            if (PageIndex > numPage) PageIndex = 1;

            var sHTML = "";

            var NaviButton = "<div class=\"fr\"><a href=\"javascript:void(0)\" class=\"cms_bm_news_btback\" id=\"" + btnPrev + "\"></a><a href=\"javascript:void(0)\" class=\"cms_bm_news_btgo\" id=\"" + btnNext + "\"></a></div>";
            var IndexTo = PageIndex * PageSize;
            var IndexForm = IndexTo - PageSize + 1;
            if (IndexTo > numRow) IndexTo = numRow;
            sHTML += "<select id=\"" + selectboxPage + "\">";
            for (var i = 1; i <= numPage; i++) {
                var iTo = i * PageSize;
                var iFrom = iTo - PageSize + 1;
                if (iTo > numRow) iTo = numRow;
                sHTML += "<option value=\"" + i + "\" " + (i == PageIndex ? "selected=\"selected\"" : "") + ">" + iFrom + '-' + iTo + "</option>";
            }
            sHTML += "</select>";
            $(objContainer).html(sHTML + NaviButton);
            $("#" + selectboxPage).change(function () { callBack(this.value, PageSize); });
            var _prevIndex = PageIndex <= 1 ? _prevIndex = 1 : PageIndex - 1;
            var _nexIndex = PageIndex >= numPage ? _nexIndex = numPage : parseInt(parseInt(PageIndex) + 1);
            if (PageIndex <= 1) {
                $("#" + btnPrev).addClass("disabled");
                $("#" + btnPrev).unbind("click");
            }
            else {
                $("#" + btnPrev).removeClass("disabled");
                $("#" + btnPrev).unbind("click").click(function () { callBack(_prevIndex, PageSize); });
            }
            if (PageIndex >= numPage) {
                $("#" + btnNext).addClass("disabled");
                $("#" + btnNext).unbind("click");
            }
            else {
                $("#" + btnNext).removeClass("disabled");
                $("#" + btnNext).unbind("click").click(function () { callBack(_nexIndex, PageSize); });
            }
        }
        function AddProductToBill() {
            $('.ProductItem input:checked').each(function (i, item) {
                var id = $(this).attr('productid');
                var $this = $(this);
                if ($('.ProductInBill[productid="' + id + '"]').length == 0) {
                    var thumbnail = $this.parents('tr:first').find('.ProductThumbnail').attr('src');
                    var name = $this.parents('tr:first').find('.ProductName').text();
                    var price = $this.parents('tr:first').find('.ProductPrice').text();
                    var point = $this.parents('tr:first').find('.ProductPoint').text();
                    var pnumber = $this.parents('tr:first').find('.ProductNumber').text();
                    $('#ListProductInBill tr:first').after('<tr productid="' + id + '" class="ProductInBill"><td><img src="' + thumbnail + '" class="ProductThumbnail"/></td><td>' + pnumber + '</td><td><span class="ProductName">' + name + '</span></td><td><span class="ProductPrice">' + price + '</span></td><td><span class="ProductPoint">' + point + '</span></td><td><input type="text" value="1" /></td></tr>');
                }
            });
        }
        function CreateBill() {
            if ($('#UserIDTxt').val() == '') {
                alert('Please Input an UserID!'); return false;
            }
            if ($('#DepoIDTxt').val() == '') {
                alert('Please Input a DepoID!'); return false;
            }
            var productIds = '';
            var quantities = '';
            $('.ProductInBill').each(function () {
                productIds += $(this).attr('productid') + ';';
                quantities += $(this).find('input[type="text"]').val() + ';';
            });
            $.ajax({
                type: "POST",
                url: "/AdminCP/Pages/CreateBill.aspx/CreateNewBill",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function () {
                    $('body').append('<div id="loadingDiv" style="position:fixed; top:50%; left:50%; width:200px;text-align:center; padding:3px; border:solid 1px #ccc; background:#f1f1f1;">Loading, please wait...</div>');
                },
                data: "{'_userId':'" + $('#UserIDTxt').val() + "','_productIds':'" + productIds + "','_quantities':'" + quantities + "','_depoId':'" + $('#DepoIDTxt').val() + "'}",
                async: true,
                success: function (msj) {
                    alert('Create Bill successfully!');
                }
            });
        }
        $(function () {
            GetListProduct();
        });
    </script>
</asp:Content>
